{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 接口地址： https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\n",
    "* 返回格式： json\n",
    "* 请求方式： post\n",
    "* 接口备注： 植物识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.32583761fc70107c8faaa523e7aedee2.315360000.1972840848.282335-26671375', 'expires_in': 2592000, 'session_key': '9mzdWu7SrQPUG3TDWNPJCbOeniXO/ZhYWF5IaWL0Tz2+Qol65xIStZJs/6zJ/6kdSipR3EO2yNitmnaUnD8/kc7eAloi5A==', 'access_token': '24.a2301676d60c556615e7ebf7a11df51f.2592000.1660072848.282335-26671375', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '6ffaa3cfab9db5b94663b661d47a78a9'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK    \n",
    "SK='hfdfx6XvzEuQZuYRHlroIbcx2Wbscqcj'\n",
    "AK='sHCSvNGIrdb3arV7YubXoOig'\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':AK,\n",
    "    'client_secret':SK\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "plant_AI = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.7465197, 'name': '牡丹', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%89%A1%E4%B8%B9/6080', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/3c6d55fbb2fb43160ee185da2aa4462308f7d390', 'description': '牡丹(学名：Paeonia suffruticosa Andr.)：是双子叶植物纲、芍药科、芍药属植物。为多年生落叶灌木，茎高达2米；分枝短而粗。叶通常为二回三出复叶，表面绿色，无毛，背面淡绿色，有时具白粉，叶柄长5-11厘米，和叶轴均无毛。花单生枝顶，苞片5，长椭圆形；萼片5，绿色，宽卵形，花瓣5或为重瓣，玫瑰色、红紫色、粉红色至白色，通常变异很大，倒卵形，顶端呈不规则的波状；花药长圆形，长4毫米；花盘革质，杯状，紫红色；心皮5，密生柔毛。蓇葖长圆形，密生黄褐色硬毛。花期5月；果期6月。花色泽艳丽，玉笑珠香，风流潇洒，富丽堂皇，素有“花中之王”的美誉。在栽培类型中，主要根据花的颜色，可分成上百个品种。牡丹品种繁多，色泽亦多，以黄、绿、肉红、深红、银红为上品，尤其黄、绿为贵。牡丹花大而香，故又有“国色天香”之称。唐代刘禹锡有诗曰：“庭前芍药妖无格，池上芙蕖净少情。唯有牡丹真国色，花开时节动京城。”在清代末年，牡丹就曾被当作中国的国花。1985年5月牡丹被评为中国十大名花之二。是中国特有的木本名贵花卉，有数千年的自然生长和1500多年的人工栽培历史。在中国栽培甚广，并早已引种世界各地。牡丹花被拥戴为花中之王，有关文化和绘画作品很丰富。2019年7月15日，中国花卉协会发出《投票：我心中的国花》，向公众征求对中国国花的意向。截至2019年7月22日24时，投票总数362264票，牡丹胜出，得票高达79.71%(概述图参考资料：)'}}, {'score': 0.72831726, 'name': '牡丹花', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%89%A1%E4%B8%B9%E8%8A%B1/648133', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/a686c9177f3e67096d4c64653ec79f3df9dc55df', 'description': '牡丹花，中药名。为毛茛科芍药属植物牡丹Paeonia suffruticosa Andr.的花。目前全国栽培甚广，并早已引种国外。常用于妇女月经不调，经行腹痛。'}}, {'score': 0.0426803, 'name': '芍药', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%8A%8D%E8%8D%AF/328161', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/9922720e0cf3d7ca7bcb5b502556a9096b63f62403c9', 'description': '芍药(学名：Paeonia lactiflora Pall.)是毛茛科、芍药属的多年生草本植物。根粗壮，分枝黑褐色。茎高40-70厘米，无毛。下部茎生叶为二回三出复叶，上部茎生叶为三出复叶；小叶狭卵形，椭圆形或披针形。花数朵，生茎顶和叶腋，有时仅顶端一朵开放；苞片4-5，披针形，大小不等；萼片4，宽卵形或近圆形；花瓣9-13，倒卵形，花瓣各色，有时基部具深紫色斑块；花丝长0.7-1.2厘米，黄色。蓇葖长2.5-3厘米，直径1.2-1.5厘米，顶端具喙。花期5-6月；果期8月。分布于中国、朝鲜、日本、蒙古及俄罗斯(西伯利亚地区)；在中国分布于江苏、东北、华北、陕西及甘肃南部，四川、贵州、安徽、山东、浙江等省及各城市公园也有栽培。在中国东北生长于海拔480-700米的山坡草地及林下，在其它各省生长于海拔1000-2300米的山坡草地。芍药是既能药用，又能供观赏的经济植物之一。根药用，称“白芍”，能镇痛、镇痉、祛瘀、通经；种子含油量约25%，供制皂和涂料用。(概述图参考来源：)'}}, {'score': 0.041377574, 'name': '花牡丹', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%8A%B1%E7%89%A1%E4%B8%B9/4471933', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/314e251f95cad1c8ac6565bf7d3e6709c83d514f', 'description': '花牡丹：是山茶科植物，常绿灌木，树形矮胖，枝展均匀。叶面深绿，背面浅绿。多数植株叶面有黄斑，花单生成对生于叶腋或枝顶，花瓣近于圆形，植株形姿优美，叶浓绿而光泽，花形艳丽缤纷。通常单生，老株个别的也会出现多头，体色灰绿，株幅17-20厘米，疣状突起呈钝三角形，叶疣末端附生白色绒点，秋季顶生白色或淡粉色漏斗状花。花期长，单花开放时间20天以上，植株越大花越大。花色艳丽，鲜红底色上撒有白色斑块，花瓣排列3-4轮，瓣片明显起皱， 金花蕾有秩的穿插其中，红 白 三色相应，美不胜收，极具观赏价值，为山茶十大名种之一。原产墨西哥高山地区，现分布于浙江温州。'}}, {'score': 0.012822298, 'name': '二乔牡丹', 'baike_info': {}}], 'log_id': 1546212956352205934}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('mudanhua.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':plant_AI,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
